% CONVOLUTION DEMO
% ----------------
% This program is demonstrating the convolution of the discreet signal and the 
% windowed sinc function. It first shows the convolution between single pixel 
% and the kernel, then sums it us for 6 pixel signal.
% The pixel domain is represented by 512 subsamples for precise numeric calculation.
figure(1)
clf
%
x=linspace(-2*pi,2*pi,1024);
for i=1:1024
	h(i)=sin(x(i))/x(i);
endfor
%
subplot(2,2,1)
plot(h),grid,xlabel('x-dimension'),ylabel('Intensity'),title('sinc kernel (not windowed)');

% Primitive approach to construct the pulse signal. Obsoleted by zeros and ones fucntions.
%for i=1:1024
%	if( i < 256 )
%		f0(i) = 0;
%	endif
%	if( (i >= 256) && (i <= 768))
%		f0(i) = 1;
%	endif
%	if( i > 768 )
%		f0(i) = 0;
%	endif		
%endfor
f0 = [zeros(1,256) ones(1,512) zeros(1,256)];

% Spacer simplified by means of zeros function.
%spacer=[0:1:512];
%spacer=0*spacer;
spacer=zeros(1,512);

%
% Generating different intensities for all 6 pixels.
f1=3*f0;
f2=2*f0;
f3=3*f0;
f4=4*f0;
f5=2*f0;

% Compiling the resulting signal
fd0=[f0 spacer spacer spacer spacer spacer];
fd1=[spacer f1 spacer spacer spacer spacer];
fd2=[spacer spacer f2 spacer spacer spacer];
fd3=[spacer spacer spacer f3 spacer spacer];
fd4=[spacer spacer spacer spacer f4 spacer];
fd5=[spacer spacer spacer spacer spacer f5];
fd=fd0+fd1+fd2+fd3+fd4+fd5;

% Plot the signal.
subplot(2,2,2)
plot(f0),grid,title('single signal level');
subplot(2,2,3)
plot(fd),grid,title('combined 6 level signal (1,3,2,3,4,2)');

%a Calculate convolution for all pixes (sliding window of sinc kernel).
g0=conv(f0,h);
g1=conv(f1,h);
g2=conv(f2,h);
g3=conv(f3,h);
g4=conv(f4,h);
g5=conv(f5,h);

% Sumarizing the envelope fucntion from single conributions.
j0=[g0 spacer spacer spacer spacer spacer];
j1=[spacer g1 spacer spacer spacer spacer];
j2=[spacer spacer g2 spacer spacer spacer];
j3=[spacer spacer spacer g3 spacer spacer];
j4=[spacer spacer spacer spacer g4 spacer];
j5=[spacer spacer spacer spacer spacer g5];

% Plotting everything ...
subplot(2,2,4)
plot(g0),grid,title('single sample convolution'),axis([0 2400 -100 400]);
%subplot(2,3,4)
%plot(f1)
%
g=j0+j1+j2+j3+j4+j5;
figure(2)
clf
plot(j0,"2", j1, "3", j2,"5", j3, "2", j4, "3", j5, "5", g,"4"),grid,xlabel('x-dimension'),ylabel('intensity'),title('envelop for 3 discreet convolutions of sinc with signal discreet points'),set(gcf,'position',[10 10 900 600]);
%plot(g,"4"),grid,xlabel('x-dimension'),ylabel('intensity'),title('envelop for 3 discreet convolutions of sinc with signal discreet points'),set(gcf,'position',[10 10 900 600]);

